//
//  Card.m
//  SpriteBatches
//
//  Created by Steffen Itterheim on 04.08.10.
//  Copyright 2010 Steffen Itterheim. All rights reserved.
//

#import "Card.h"


@interface Card (PrivateMethods)
-(id) initWithCardImage;
@end


@implementation Card

+(id) card
{
	return [[[self alloc] initWithCardImage] autorelease];
    
}



-(id) initWithCardImage
{
    //self.isTouchEnabled = YES;//

    m_bHideAnimation = false;
    m_eState = STATE_REST;
	if ((self = [super initWithFile:@"frame1.png"]))
	{
		
        //----CARTA BLANCA (frame 1-7)
		NSMutableArray* frames = [NSMutableArray arrayWithCapacity:6];
		for (int i = 1; i < 7	; i++)
		{
			NSString* file = [NSString stringWithFormat:@"frame%i.png", i];
			CCTexture2D* texture = [[CCTextureCache sharedTextureCache] addImage:file];
			CGSize texSize = texture.contentSize;
			CGRect texRect = CGRectMake(0, 0, texSize.width, texSize.height);
			CCSpriteFrame* frame = [CCSpriteFrame frameWithTexture:texture rect:texRect offset:CGPointZero];
			[frames addObject:frame];
		}
		// create an animation object from all the sprite animation frames
		CCAnimation* anim = [CCAnimation animationWithName:@"showCards_1" delay:0.04f frames:frames];
		// add the animation to the sprite
		[self addAnimation:anim];
        
        
        //----CARTA BLANCA (frame 7-15)
        NSMutableArray* frames2 = [NSMutableArray arrayWithCapacity:9];
		for (int i = 7; i < 16; i++)
		{
			NSString* file = [NSString stringWithFormat:@"frame%i.png", i];
			CCTexture2D* texture = [[CCTextureCache sharedTextureCache] addImage:file];
			CGSize texSize = texture.contentSize;
			CGRect texRect = CGRectMake(0, 0, texSize.width, texSize.height);
			CCSpriteFrame* frame = [CCSpriteFrame frameWithTexture:texture rect:texRect offset:CGPointZero];
			[frames2 addObject:frame];
		}
		// create an animation object from all the sprite animation frames
		CCAnimation* anim2 = [CCAnimation animationWithName:@"showCards_2" delay:0.04f frames:frames2];
		// add the animation to the sprite
		[self addAnimation:anim2];
        
        
        m_pNumLeft = [CCSprite spriteWithFile:@"left_frame_7_card_39.png"]; 
        m_pNumRight = [CCSprite spriteWithFile:@"right_frame_7_card_27.png"]; 
        m_pNumRight.position = CGPointMake(256, 128);
        m_pNumLeft.position = CGPointMake(256, 128);
        
        
        for (int index_card = 2; index_card < 52; index_card++)
        {
            //-------CARTA IZQUIERDA-------
            //Els frames de las cartes tenen la següent sintaxi -> right_frame_13_card_2.png, on 13 indica el número de frame i el 2 la carta que es vol mostrar.
            NSMutableArray* frames3 = [NSMutableArray arrayWithCapacity:9];
            for (int i = 7; i < 16; i++)
            {
                NSString* file = [NSString stringWithFormat:@"left_frame_%i_card_%i.png", i, index_card];
                CCTexture2D* texture = [[CCTextureCache sharedTextureCache] addImage:file];
                CGSize texSize = texture.contentSize;
                CGRect texRect = CGRectMake(0, 0, texSize.width, texSize.height);
                CCSpriteFrame* frame = [CCSpriteFrame frameWithTexture:texture rect:texRect offset:CGPointZero];
                [frames3 addObject:frame];
            }
            NSString* animation_card_left = [NSString stringWithFormat:@"show_Numbers_Left_%i", index_card]; 
            CCAnimation* anim3 = [CCAnimation animationWithName:animation_card_left delay:0.04f frames:frames3];
            // add the animation to the sprite
            [m_pNumLeft addAnimation:anim3];
            
            
            
            //-------CARTA DERECHA-------
            NSMutableArray* frames4 = [NSMutableArray arrayWithCapacity:9];
            for (int i = 7; i < 16; i++)
            {
                NSString* file = [NSString stringWithFormat:@"right_frame_%i_card_%i.png", i, index_card];
                CCTexture2D* texture = [[CCTextureCache sharedTextureCache] addImage:file];
                CGSize texSize = texture.contentSize;
                CGRect texRect = CGRectMake(0, 0, texSize.width, texSize.height);
                CCSpriteFrame* frame = [CCSpriteFrame frameWithTexture:texture rect:texRect offset:CGPointZero];
                [frames4 addObject:frame];
            }
            NSString* animation_card_right = [NSString stringWithFormat:@"show_Numbers_Right_%i", index_card]; 
            // create an animation object from all the sprite animation frames
            CCAnimation* anim4 = [CCAnimation animationWithName:animation_card_right delay:0.04f frames:frames4];
            // add the animation to the sprite
            [m_pNumRight addAnimation:anim4];
            
        }
        [self addChild:m_pNumLeft];
        [self addChild:m_pNumRight];
        
        
        m_pNumRight.visible = false;
        m_pNumLeft.visible = false;        
		
	}
	return self;
}

-(void) showCards: (int) rightIndex andLeftIndex:(int) leftIndex
{
    if( m_eState == STATE_REST)
    {
        m_iAnimation_cardRight  = rightIndex; 
        m_iAnimation_cardLeft   = leftIndex; 
        //NSLog(@"%@, %@", m_sAnimation_cardLeft, m_sAnimation_cardRight);
        
        m_eState = STATE_SHOW_CARD_1;
        
        CCAnimation* moveAnim = [self animationByName:@"showCards_1"];
        CCAnimate* animate = [CCAnimate actionWithAnimation:moveAnim restoreOriginalFrame:NO];
        CCCallFunc* func = [CCCallFunc actionWithTarget:self selector:@selector(onFinishShowCard1)];
        CCSequence* seq = [CCSequence actions:animate, func, nil]; 
        [self runAction:seq];
    }
}

-(void) onFinishShowCard1
{
    CCLOG(@"end of showCard1!");
    if (!m_bHideAnimation)
    {
        m_eState = STATE_SHOW_CARD_2;
        CCAnimation* moveAnim = [self animationByName:@"showCards_2"];
        CCAnimate* animate = [CCAnimate actionWithAnimation:moveAnim restoreOriginalFrame:NO];
        CCCallFunc* func = [CCCallFunc actionWithTarget:self selector:@selector(onFinishShowCard2)];
        CCSequence* seq = [CCSequence actions:animate, func, nil]; 
        [self runAction:seq];
        
        
        m_pNumRight.visible = true;
        m_pNumLeft.visible = true;      
        
        
        NSString * cardRight = [NSString stringWithFormat:@"show_Numbers_Right_%i", m_iAnimation_cardRight];
        NSString * cardLeft = [NSString stringWithFormat:@"show_Numbers_Left_%i", m_iAnimation_cardLeft];
        CCAnimation* moveAnim2 = [m_pNumLeft animationByName:cardLeft];
        CCAnimate* animate2 = [CCAnimate actionWithAnimation:moveAnim2 restoreOriginalFrame:NO];
        CCSequence* seq2 = [CCSequence actions:animate2, nil]; 
        [m_pNumLeft runAction:seq2];
        
        CCAnimation* moveAnim3 = [m_pNumRight animationByName:cardRight];
        CCAnimate* animate3 = [CCAnimate actionWithAnimation:moveAnim3 restoreOriginalFrame:NO];
        CCSequence* seq3 = [CCSequence actions:animate3, nil]; 
        [m_pNumRight runAction:seq3];

    }
    else
    {
        m_bHideAnimation = false;
        m_eState = STATE_SHOW_BACK;
        CCAnimation* moveAnim = [self animationByName:@"showCards_1"];
        CCAnimate* animate = [CCAnimate actionWithAnimation:moveAnim restoreOriginalFrame:NO];
        id action_back = [animate reverse]; 
        CCCallFunc* func = [CCCallFunc actionWithTarget:self selector:@selector(onFinishBackShowCard1)];
        CCSequence* seq = [CCSequence actions:action_back, func, nil]; 
        [self runAction:seq];

    }
}
-(void) onFinishShowCard2
{
    m_eState = STATE_SHOW_ALL;
    CCLOG(@"end of showCard2!");
    if (m_bHideAnimation)
    {
        m_bHideAnimation = false;
        m_eState = STATE_SHOW_BACK;
        
          
        //llamar a la animacion esconder_cartas2 y concatenarla con esconder_cartas1
        CCAnimation* moveAnim = [self animationByName:@"showCards_2"];
        CCAnimate* animate = [CCAnimate actionWithAnimation:moveAnim restoreOriginalFrame:NO];
        id action_back = [animate reverse]; 
        CCAnimation* moveAnim2 = [self animationByName:@"showCards_1"];
        CCAnimate* animate2 = [CCAnimate actionWithAnimation:moveAnim2 restoreOriginalFrame:NO];
        id action_back2 = [animate2 reverse]; 
        CCCallFunc* func = [CCCallFunc actionWithTarget:self selector:@selector(onFinishBackShowCard1)];
        CCCallFunc* func2 = [CCCallFunc actionWithTarget:self selector:@selector(onFinishBackShowCard2)];
        CCSequence* seq = [CCSequence actions:action_back, func2, action_back2, func, nil]; 
        
        [self runAction:seq];   
    
        NSString * cardRight = [NSString stringWithFormat:@"show_Numbers_Right_%i", m_iAnimation_cardRight];
        NSString * cardLeft = [NSString stringWithFormat:@"show_Numbers_Left_%i", m_iAnimation_cardLeft];
        
        CCAnimation* moveAnim3 = [m_pNumLeft animationByName:cardLeft];
        CCAnimate* animate3 = [CCAnimate actionWithAnimation:moveAnim3 restoreOriginalFrame:NO];
        id action_back3 = [animate3 reverse]; 
        [m_pNumLeft runAction:action_back3];
        
        CCAnimation* moveAnim4 = [m_pNumRight animationByName:cardRight];
        CCAnimate* animate4 = [CCAnimate actionWithAnimation:moveAnim4 restoreOriginalFrame:NO];
        id action_back4 = [animate4 reverse];
        [m_pNumRight runAction:action_back4];

    }
}

-(void) onFinishBackShowCard1
{
    CCLOG(@"end of bacShowCard1!");
    m_eState = STATE_REST;
}

-(void) onFinishBackShowCard2
{
    CCLOG(@"end of bacShowCard2!");
    m_pNumRight.visible = false;
    m_pNumLeft.visible = false;      
}

-(void) hideCards
{
   
    if (m_eState == STATE_SHOW_ALL)
    {
        m_eState = STATE_SHOW_BACK;

         
        
        //llamar a la animacion esconder_cartas2 y concatenarla con esconder_cartas1
        CCAnimation* moveAnim = [self animationByName:@"showCards_2"];
        CCAnimate* animate = [CCAnimate actionWithAnimation:moveAnim restoreOriginalFrame:NO];
        id action_back = [animate reverse]; 
        CCAnimation* moveAnim2 = [self animationByName:@"showCards_1"];
        CCAnimate* animate2 = [CCAnimate actionWithAnimation:moveAnim2 restoreOriginalFrame:NO];
        id action_back2 = [animate2 reverse]; 
        CCCallFunc* func = [CCCallFunc actionWithTarget:self selector:@selector(onFinishBackShowCard1)];
        CCCallFunc* func2 = [CCCallFunc actionWithTarget:self selector:@selector(onFinishBackShowCard2)];
        CCSequence* seq = [CCSequence actions:action_back, func2, action_back2, func, nil]; 
        [self runAction:seq];   
        
        NSString * cardRight = [NSString stringWithFormat:@"show_Numbers_Right_%i", m_iAnimation_cardRight];
        NSString * cardLeft = [NSString stringWithFormat:@"show_Numbers_Left_%i", m_iAnimation_cardLeft];

        CCAnimation* moveAnim3 = [m_pNumLeft animationByName:cardLeft];
        CCAnimate* animate3 = [CCAnimate actionWithAnimation:moveAnim3 restoreOriginalFrame:NO];
        id action_back3 = [animate3 reverse]; 
        [m_pNumLeft runAction:action_back3];
        
        CCAnimation* moveAnim4 = [m_pNumRight animationByName:cardRight];
        CCAnimate* animate4 = [CCAnimate actionWithAnimation:moveAnim4 restoreOriginalFrame:NO];
        id action_back4 = [animate4 reverse];
        [m_pNumRight runAction:action_back4];

    }
    else if (m_eState== STATE_SHOW_CARD_1 || m_eState == STATE_SHOW_CARD_2)
    {
        m_bHideAnimation = true;
    }
}

-(void) dealloc
{
	
	// don't forget to call "super dealloc"
	[super dealloc];
}



@end
